package com.roam.controller;

import com.roam.VO.UserLoginVO;
import com.roam.VO.VisitBackVO;
import com.roam.VO.VisitVO;
import com.roam.entity.*;
import com.roam.jwt.Jwt;
import com.roam.result.Result;
import com.roam.service.SendMessageService;
import com.roam.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@CrossOrigin
public class UserController {
    @Value("${upload.dir}")
    private String uploadDir;


    @Autowired
    private UserService userService;
    @Autowired
    SendMessageService sendMessageService;
    @PostMapping("/login/get")
    public Result login(@RequestBody User user){


        User u = userService.login(user);


        if(u!=null){
            Map<String,Object> claims= new HashMap<>();
            claims.put("email",u.getEmail());
            String jwt = Jwt.generateJwt(claims);
            UserLoginVO userLoginVO=UserLoginVO.builder()
                    .userID(u.getUserID())
                    .token(jwt)
                    .build();
            return Result.success(userLoginVO);
        }
        return Result.error("账号或密码错误");

    }
    @PostMapping("/regist/post")
    public Result sendMail(@RequestBody EmailOnly email){

       return sendMessageService.sendMessage(email);
    }
    @PostMapping("/regist/get")
    public Result testCheckCode(@RequestBody RegisterMail registerMail){
        return sendMessageService.checkCode(registerMail);
    }

    @PostMapping("/userMessage/get")
    public Result getUserConcreteMessage(@RequestBody User user){
        UserConcreteInfo userConcreteInfo=userService.getConcreteInfo(user.getUserID());
        if(userConcreteInfo.getPassword()==null) {
            userConcreteInfo.setPassword("");
        }
        Result result = new Result<>();
        result.setMsg("");
        result.setCode(1);
        result.setData(userConcreteInfo);
        return result;
    }

    //下述接口用于标记景点为已去过
    @PostMapping("/scene/visit")
    public VisitBackVO markAttraction(@RequestBody VisitVO visitVO){
        int useId = visitVO.getUserID();
        String city = visitVO.getCity();
        int sceneId = visitVO.getSceneID();
        userService.markPlaceVisit(useId,city,sceneId);
        return VisitBackVO.success();
    }

    //下述接口用于修改用户信息
    @PostMapping("/correctMessage/post")
    public  Result correctUserInfo(@RequestBody UserConcreteUsebyUpdate userConcreteUsebyUpdate){
        if(userService.updateUserInfo(userConcreteUsebyUpdate)){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    @PostMapping("/uploadAvatar")
    public Result uploadFile(@RequestParam("avatar") MultipartFile file, @RequestParam("userID") int userID) {
        String url = userService.saveAvatar(userID,file);
        if(!url.equals("保存失败")){
            Result result = new Result<>(1,"保存成功",url);
            return result;
        }
        return Result.error(url);
    }
}
